/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.fragment.zhuanquanquan;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.core.PageOption;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.edittext.ValidatorEditText;
import com.xuexiang.xuidemo.R;
import com.xuexiang.xuidemo.adapter.zhuanquanquan.DecisionAdapter;
import com.xuexiang.xuidemo.base.BaseFragment;
import com.xuexiang.xuidemo.entity.Decision;
import com.xuexiang.xuidemo.entity.ResultAndWeight;
import com.xuexiang.xuidemo.utils.ZhuanpanSPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Page(name = "修改决定")
public class SetDecisionFragment extends BaseFragment {


    @BindView(R.id.edit_title)
    ValidatorEditText editText;

    @BindView(R.id.list_add_decision)
    ListView listView;

    @BindView(R.id.btn_add_decision)
    Button button;

    List<ResultAndWeight> list;
    DecisionAdapter listAdapter;
    Decision decision;

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.addAction(new TitleBar.ImageAction(R.drawable.ic_decision_submit) {
            @Override
            @SingleClick
            public void performAction(View view) {
                ZhuanpanSPUtils zInstance = ZhuanpanSPUtils.getInstance();
                decision.setResults(new ArrayList<>());
                decision.setWeights(new ArrayList<>());
                for(int i=0;i<list.size();i++) {
                    decision.getResults().add(list.get(i).getResult());
                    if(list.get(i).getWeight()<=0)
                        list.get(i).setWeight(1);
                    decision.getWeights().add(list.get(i).getWeight());
                }
                decision.setNum(decision.getResults().size());
                zInstance.updateDecision(decision);
                popToBack();
            }
        });
        return titleBar;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_decision;
    }

    @Override
    protected void initViews() {
        Bundle bundle = getArguments();
        if(bundle.containsKey("key")){
            int key = bundle.getInt("key");
            ZhuanpanSPUtils zInstance = ZhuanpanSPUtils.getInstance();
            decision = zInstance.getDecisionById(key);
        }
        else decision = null;
        initList();
        listAdapter = new DecisionAdapter(getContext(),R.layout.item_result_weight,list);
        listView.setAdapter(listAdapter);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //listAdapter.add(new ResultAndWeight("",1));
                list.add(new ResultAndWeight("",1));
                listAdapter.notifyDataSetChanged();
            }
        });
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //Toast.makeText(getContext(),s.toString(),Toast.LENGTH_SHORT).show();
                decision.setTitle(s.toString());
            }
        });
    }

    private void initList() {
        list = new ArrayList<>();
        if(decision == null) {
            decision = new Decision(0,"",new ArrayList<String>(),new ArrayList<Integer>());
            list.add(new ResultAndWeight("",1));
            list.add(new ResultAndWeight("",1));
        }
        else {
            for(int i=0;i<decision.getResults().size();i++)
            {
                editText.setText(decision.getTitle());
                list.add(new ResultAndWeight(decision.getResults().get(i),decision.getWeights().get(i)));
            }
        }
    }


}
